package com.hnkywl.sts.action.report;

import com.hnkywl.sts.entity.organ.Gsjg;
import com.hnkywl.sts.entity.sys.Gncz;
import com.hnkywl.sts.service.organ.GsjgService;
import com.hnkywl.sts.service.report.CwsfcService;
import com.hnkywl.sts.service.sys.GnczService;
import com.hnkywl.sts.util.POIUtil;
import net.ruixin.orm.hibernate.Page;
import net.ruixin.web.struts2.SimpleActionSupport;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Namespaces;
import org.springframework.beans.factory.annotation.Autowired;

import java.text.DateFormat;
import java.util.*;

/**
 * Created with IntelliJ IDEA.
 * User: Administrator
 * Date: 13-6-14
 * Time: 下午4:48
 * To change this template use File | Settings | File Templates.
 */
@Namespace("/report/cwsfc")
public class CwsfcAction extends SimpleActionSupport {
    private Long gsId;
    private String type;
    private Integer year;
    private Integer month;
    private Date startDate;
    private Date endDate;
    private Date date;
    private Integer codeId;
    private int page;
    private int rows;

    private GsjgService gsjgService;
    private CwsfcService cwsfcService;

    //功能操作
    private List<Gncz> gnczList;
    private GnczService gnczService;


    @Action("cwsfc")
    public String cwsfc() {
        gnczList = gnczService.findGnczByJsAndUrl(request.getServletPath());
        date = new Date();
        return SUCCESS;
    }

    @Action("cwsfcList")
    public void cwsfcList() {
        String json = cwsfcService.cwsfcList(gsId, year, month);
        renderJson(json);
    }

    @Action("cbjz")
    public void cbjz() {
        try {
            String errorMsg = cwsfcService.cbjz(year, month);
            if (StringUtils.isEmpty(errorMsg))
                renderJson("{\"statusCode\":\"200\", \"message\":\"操作成功!\"}");
            else
                renderJson("{\"statusCode\":\"300\", \"message\":\"" + errorMsg + "\"}");
        } catch (Exception e) {
            e.printStackTrace();
            renderJson("{\"statusCode\":\"300\", \"message\":\"" + e.getMessage() + "\"}");
        }
    }

    @Action("ymjz")
    public void ymjz() {
        try {
            String errorMsg = cwsfcService.sfcymjz(year, month);
            if (StringUtils.isEmpty(errorMsg))
                renderJson("{\"statusCode\":\"200\", \"message\":\"操作成功!\"}");
            else
                renderJson("{\"statusCode\":\"300\", \"message\":\"" + errorMsg + "\"}");
        } catch (Exception e) {
            e.printStackTrace();
            renderJson("{\"statusCode\":\"300\", \"message\":\"" + e.getMessage() + "\"}");
        }
    }

    /**
     * 明细页面
     *
     * @return
     */
    @Action("detail")
    public String detail() {
        if (year == null || month == null) {
            Calendar c = Calendar.getInstance();
            startDate = DateUtils.truncate(c.getTime(), 2);
            c.add(Calendar.MONTH, 1);
            c.add(Calendar.DATE, -1);
            endDate = DateUtils.truncate(c.getTime(), 2);
            c.setTime(endDate);
            c.add(Calendar.DATE, -1);
            endDate = c.getTime();
        } else {
            Calendar c = Calendar.getInstance();
            c.set(year, month - 1, 1);
            startDate = c.getTime();
            c.add(Calendar.MONTH, 1);
            endDate = DateUtils.truncate(c.getTime(), 2);
            c.setTime(endDate);
            c.add(Calendar.DATE, -1);
            endDate = c.getTime();
        }
        return SUCCESS;
    }

    /**
     * 明细页面表数据
     */
    @Action("detailList")
    public void detailList() {
        renderJson(cwsfcService.cwsfcDetailList(gsId, type, -1, startDate, endDate, page, rows));
    }

    @Action("exportDetail")
    public String exportDetail() {
        Map<String, Object> map = cwsfcService.cwsfcDetailListExport(gsId, type, -1, startDate, endDate);
        List<Map> rows = (ArrayList) map.get("rows");
        List<Map> footer = (ArrayList) map.get("footer");

        HSSFWorkbook wb = new HSSFWorkbook();
        HSSFSheet sheet = wb.createSheet("sheet1");
        POIUtil poiUtil = new POIUtil(wb);
        sheet.setDefaultRowHeightInPoints(21);
        sheet.setDefaultColumnWidth((short) 20);
        HSSFCellStyle headerStyle = poiUtil.createHeader3HSSFCellStyle(wb);

        Map<String, Object> options = exportOptions(type, gsId, endDate);
        List<String> columns = (List<String>) options.get("columns");
        List<String> columnName = (List<String>) options.get("columnName");
        List<Integer> columnScale = (List<Integer>) options.get("columnScale");
        String fileName = (String) options.get("fileName");

        poiUtil.fillRow(sheet, headerStyle, 0, columnName.toArray());
        for (int i = 1; i < rows.size() + 1; i++) {
            Map item = rows.get(i - 1);
            List<Object> data = new ArrayList<Object>();
            for (String col : columns) {
                data.add(item.get(col));
            }
            poiUtil.fillRowComplex(wb, sheet, null, i, columnScale, data.toArray());
        }
        //统计行
        if (footer != null) {
            HSSFCellStyle sumStyle = wb.createCellStyle();
            HSSFFont font = wb.createFont();
            font.setColor(HSSFFont.COLOR_RED);
            sumStyle.setFont(font);
            Map f = footer.get(0);
            List<Object> footerData = new ArrayList<Object>();
            for (String col : columns) {
                footerData.add(f.get(col));
            }
            poiUtil.fillRow(sheet, sumStyle, rows.size() + 1, footerData.toArray());
        }
        try {
            response.setContentType("application/x-download");
            String filedisplay = new String((fileName).getBytes("gb2312"), "iso8859-1");
            response.addHeader("Content-Disposition", "attachment;filename=" + filedisplay);
            wb.write(response.getOutputStream());
            response.getOutputStream().close();
        } catch (Exception e) {
            e.printStackTrace();
            renderText("位置异常，导出失败" + e.getMessage());
        }
        return null;
    }

    private Map<String, Object> exportOptions(String type, Long gsid, Date date) {
        Gsjg gsjg = gsjgService.findGsjgDetail(gsid);
        List columns = new ArrayList();
        List columnName = new ArrayList();
        List columnScale = new ArrayList();
        String fileName = "";
        DateFormat dateFormat = new java.text.SimpleDateFormat("yyyyMMddHHmmss");
        String s = dateFormat.format(new Date());
        Integer month = date.getMonth() + 1;
        //CGJXFP 采购进项发票
        if ("cgjxfp".equals(type)) {
            String[] temp = {"gsmc", "cgdh", "gysmc", "code", "name", "ggxh", "th", "dw", "rksl", "jshsj", "jsbhsj", "jshse", "jsse", "jsbhse"};
            columns = Arrays.asList(temp);
            temp = new String[]{"公司名称", "采购单号", "供应商", "物资编码", "品名", "规格", "图号", "单位", "入库数量", "含税单价", "不含税单价", "含税总金额", "税额", "净余额"};
            columnName = Arrays.asList(temp);
            Integer[] scale = {0, 0, 0, 0, 0, 0, 0, 0, 4, 4, 4, 2, 2, 2};
            columnScale = Arrays.asList(scale);
            fileName = gsjg.getJc() + "_" + month + "月收发存采购结算明细_" + s + ".xls";
        }
        //WLJXFP 物流进项发票
        if ("wljxfp".equals(type)) {
            String[] temp = {"gsmc", "cgdh", "sydwmc", "fymc", "jfsl", "je", "hdje", "kdse", "jsje"};
            columns = Arrays.asList(temp);
            temp = new String[]{"公司名称", "采购单号", "物流商", "费用明细", "计费数量", "计费金额", "核定金额", "可抵税额", "净余额"};
            columnName = Arrays.asList(temp);
            Integer[] scale = {0, 0, 0, 0, 4, 2, 2, 2, 2};
            columnScale = Arrays.asList(scale);
            fileName = gsjg.getJc() + "_" + month + "月收发存物流结算明细_" + s + ".xls";
        }

        //CGJXGS 采购进项估收
        if ("cgjxgs".equals(type)) {
            String[] temp = {"gsmc", "cgdh", "gysmc", "code", "name", "ggxh", "th", "dw", "rksl", "hsj", "bhsj", "hse", "se", "bhse"};
            columns = Arrays.asList(temp);
            temp = new String[]{"公司名称", "采购单号", "供应商", "物资编码", "品名", "规格", "图号", "单位", "入库数量", "含税单价", "不含税单价", "含税总金额", "税额", "净余额"};
            columnName = Arrays.asList(temp);
            Integer[] scale = {0, 0, 0, 0, 0, 0, 0, 0, 4, 4, 4, 2, 2, 2};
            columnScale = Arrays.asList(scale);
            fileName = gsjg.getJc() + "_" + month + "月收发存采购估收明细_" + s + ".xls";
        }

        //WLJXGS物流进项估收
        if ("wljxgs".equals(type)) {
            String[] temp = {"gsmc", "cgdh", "sydwmc", "fymc", "jfsl", "je"};
            columns = Arrays.asList(temp);
            temp = new String[]{"公司名称", "采购单号", "物流商", "费用明细", "计费数量", "计费金额"};
            columnName = Arrays.asList(temp);
            Integer[] scale = {0, 0, 0, 0, 4, 2};
            columnScale = Arrays.asList(scale);
            fileName = gsjg.getJc() + "_" + month + "月收发存物流估收明细_" + s + ".xls";
        }

        //SLTZ_数量调整
        if ("sltz".equals(type)) {
            String[] temp = {"kcpdbm", "khbm", "gsqc", "ckmc", "code", "name", "ggxh", "th", "dw", "yksl", "ykje", "pdrq", "pdrxm", "shrq", "shrxm"};
            columns = Arrays.asList(temp);
            temp = new String[]{"盘点单号", "卡号", "公司名称", "仓库名称", "物资编码", "品名", "规格", "图号", "单位", "盈亏数量", "盈亏金额", "盘点日期", "盘点人", "审核日期", "审核人"};
            columnName = Arrays.asList(temp);
            Integer[] scale = {0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 3, 0, 0, 0, 0};
            columnScale = Arrays.asList(scale);
            fileName = gsjg.getJc() + "_" + month + "月收发存数量审核明细_" + s + ".xls";
        }

        //XSFP_销项发票
        if ("xsfp".equals(type)) {
            String[] temp = {"gsmc", "xsdh", "khmc", "code", "name", "ggxh", "th", "dw", "cksl", "xsdj", "je", "se", "cbje"};
            columns = Arrays.asList(temp);
            temp = new String[]{"公司名称", "销售单号", "客户名称", "物资编码", "品名", "规格", "图号", "单位", "出库数量", "销售单价", "净余额", "税额", "成本总金额"};
            columnName = Arrays.asList(temp);
            Integer[] scale = {0, 0, 0, 0, 0, 0, 0, 0, 4, 4, 2, 2, 2};
            columnScale = Arrays.asList(scale);
            fileName = gsjg.getJc() + "_" + month + "月收发存销售结算明细_" + s + ".xls";
        }

        //NBLL内部领料
        if ("nbll".equals(type)) {
            String[] temp = {"lldh", "llgs", "llbm", "llr", "ckmc", "code", "name", "ggxh", "th", "dw", "flsl", "flje", "cbje", "flgs", "flbm", "flr", "zdr"};
            columns = Arrays.asList(temp);
            temp = new String[]{"领料单号", "领料单位", "领料部门", "领料人", "仓库名称", "物资编码", "品名", "规格", "图号",
                    "单位", "发料数量", "发料金额", "成本总金额", "发料单位", "发料部门", "发料人", "制单人"};
            columnName = Arrays.asList(temp);
            Integer[] scale = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 2, 2, 0, 0, 0, 0};
            columnScale = Arrays.asList(scale);
            fileName = gsjg.getJc() + "_" + month + "月收发存内部领料明细_" + s + ".xls";
        }
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("columns", columns);
        map.put("columnName", columnName);
        map.put("columnScale", columnScale);
        map.put("fileName", fileName);
        return map;
    }

    /**
     * 采购单统计明细
     */
    @Action("yjscgdtj")
    public String cgdtjmx() {
        if (year == null || month == null) {
            date = new Date();
        } else {
            Calendar c = Calendar.getInstance();
            c.set(Calendar.YEAR, year);
            c.set(Calendar.MONTH, month - 1);
            date = c.getTime();
        }
        return SUCCESS;
    }

    @Action("cgdtjmxList")
    public void cgdtjmxList() {
        renderJson(cwsfcService.cgdtjmx(codeId, gsId, year, month, page, rows));
    }

    @Action("exportyjscgdtj")
    public String exportcgjs() {
        Map<String, Map> map = cwsfcService.cgdtjmxExp(codeId, gsId, year, month);
        Page<Map> page = (Page) map.get("page");
        List<Map> result = page.getResult();
        HSSFWorkbook wb = new HSSFWorkbook();
        HSSFSheet sheet = wb.createSheet("sheet1");
        POIUtil poiUtil = new POIUtil(wb);
        sheet.setDefaultRowHeightInPoints(21);
        sheet.setDefaultColumnWidth((short) 20);
        HSSFCellStyle headerStyle = poiUtil.createHeader3HSSFCellStyle(wb);
        poiUtil.fillRow(sheet, headerStyle, 0, "采购单号", "公司名称", "物资编码", "品名", "规格", "单位", "数量", "含税单价", "不含税单价"
                , "含税总金额", "税额", "净余额", "供应商");
        List<Integer> scale = Arrays.asList(0, 0, 0, 0, 0, 0, 4, 4, 4, 2, 2, 2, 0);
        for (int i = 1; i < result.size() + 1; i++) {
            Map item = result.get(i - 1);
            poiUtil.fillRowComplex(wb, sheet, null, i, scale, item.get("cgdh"), item.get("gsjc"), item.get("wzbm"), item.get("wzname"),
                    item.get("gg"), item.get("dw"), item.get("rksl"), item.get("jshsj"), item.get("jsbhsj"),
                    item.get("hse"), item.get("se"), item.get("jsbhse"), item.get("gys"));
        }
        HSSFCellStyle sumStyle = wb.createCellStyle();
        HSSFFont font = wb.createFont();
        font.setColor(HSSFFont.COLOR_RED);
        sumStyle.setFont(font);
        poiUtil.fillRow(sheet, sumStyle, result.size() + 1, "合计：", "", "", "", "", "", "", "", "", "", "",
                map.get("sum"), "");
        try {
            response.setContentType("application/x-download");
            String filedisplay = new String(("收发存采购结算明细.xls").getBytes("gb2312"), "iso8859-1");
            response.addHeader("Content-Disposition", "attachment;filename=" + filedisplay);
            wb.write(response.getOutputStream());
            response.getOutputStream().close();
        } catch (Exception e) {
            e.printStackTrace();
            renderText("位置异常，导出失败" + e.getMessage());
        }
        return null;
    }

    /**
     * 采购单统计明细
     */
    @Action("cggs")
    public String cggs() {
        if (year == null || month == null) {
            date = new Date();
        } else {
            Calendar c = Calendar.getInstance();
            c.set(Calendar.YEAR, year);
            c.set(Calendar.MONTH, month);
            c.set(Calendar.DAY_OF_MONTH, 0);
            date = c.getTime();
        }
        return SUCCESS;
    }

    @Action("cggsList")
    public void cggsList() {
        renderJson(cwsfcService.cgdGstjmx(codeId, gsId, date, page, rows));
    }

    @Action("exportcggs")
    public String exportcggs() {
        Map<String, Map> map = cwsfcService.cgdGstjmxExp(codeId, gsId, date);
        Page<Map> page = (Page) map.get("page");
        List<Map> result = page.getResult();
        HSSFWorkbook wb = new HSSFWorkbook();
        HSSFSheet sheet = wb.createSheet("sheet1");
        POIUtil poiUtil = new POIUtil(wb);
        sheet.setDefaultRowHeightInPoints(21);
        sheet.setDefaultColumnWidth((short) 20);
        HSSFCellStyle headerStyle = poiUtil.createHeader3HSSFCellStyle(wb);
        poiUtil.fillRow(sheet, headerStyle, 0, "采购单号", "公司名称", "物资编码", "品名", "规格", "单位", "数量", "含税单价", "不含税单价"
                , "含税总金额", "税额", "净余额", "供应商");
        List<Integer> scale = Arrays.asList(0, 0, 0, 0, 0, 0, 4, 4, 4, 2, 2, 2, 0);
        for (int i = 1; i < result.size() + 1; i++) {
            Map item = result.get(i - 1);
            poiUtil.fillRowComplex(wb, sheet, null, i, scale, item.get("cgdh"), item.get("gsjc"), item.get("wzbm"), item.get("wzname"),
                    item.get("gg"), item.get("dw"), item.get("rksl"), item.get("hsj"), item.get("bhsj"),
                    item.get("hse"), item.get("se"), item.get("bhse"), item.get("gys"));
        }
        HSSFCellStyle sumStyle = wb.createCellStyle();
        HSSFFont font = wb.createFont();
        font.setColor(HSSFFont.COLOR_RED);
        sumStyle.setFont(font);
        poiUtil.fillRow(sheet, sumStyle, result.size() + 1, "合计：", "", "", "", "", "", "", "", "", "", "", map.get("sum"), "");
        try {
            response.setContentType("application/x-download");
            String filedisplay = new String(("收发存采购估收明细.xls").getBytes("gb2312"), "iso8859-1");
            response.addHeader("Content-Disposition", "attachment;filename=" + filedisplay);
            wb.write(response.getOutputStream());
            response.getOutputStream().close();
        } catch (Exception e) {
            e.printStackTrace();
            renderText("位置异常，导出失败" + e.getMessage());
        }
        return null;
    }

    /**
     * 物流结算明细
     */
    @Action("wljs")
    public String wljs() {
        if (year == null || month == null) {
            date = new Date();
        } else {
            Calendar c = Calendar.getInstance();
            c.set(Calendar.YEAR, year);
            c.set(Calendar.MONTH, month);
            c.set(Calendar.DAY_OF_MONTH, 0);
            date = c.getTime();
        }
        return SUCCESS;
    }

    @Action("wljsList")
    public void wljsList() {
        renderJson(cwsfcService.wljsMx(codeId, gsId, year, month, page, rows));
    }

    /**
     * 物流估收明细
     */
    @Action("wlgs")
    public String wlgs() {
        if (year == null || month == null) {
            date = new Date();
        } else {
            Calendar c = Calendar.getInstance();
            c.set(Calendar.YEAR, year);
            c.set(Calendar.MONTH, month);
            c.set(Calendar.DAY_OF_MONTH, 0);
            date = c.getTime();
        }
        return SUCCESS;
    }

    @Action("wlgsList")
    public void wlgsList() {
        renderJson(cwsfcService.wlgsMx(codeId, gsId, date, page, rows));
    }

    @Action("exportwlgs")
    public String exportwlgs() {
        Map<String, Map> map = cwsfcService.wlGstjmxExp(codeId, gsId, date);
        Page<Map> page = (Page) map.get("page");
        List<Map> result = page.getResult();
        HSSFWorkbook wb = new HSSFWorkbook();
        HSSFSheet sheet = wb.createSheet("sheet1");
        POIUtil poiUtil = new POIUtil(wb);
        sheet.setDefaultRowHeightInPoints(21);
        sheet.setDefaultColumnWidth((short) 20);
        HSSFCellStyle headerStyle = poiUtil.createHeader3HSSFCellStyle(wb);
        poiUtil.fillRow(sheet, headerStyle, 0, "采购单号", "公司名称", "收益单位", "费用类别", "数量", "总金额", "可抵税额", "抵税额", "净余额");
        List<Integer> scale = Arrays.asList(0, 0, 0, 0, 4, 2, 2, 2, 2);
        for (int i = 1; i < result.size() + 1; i++) {
            Map item = result.get(i - 1);
            poiUtil.fillRowComplex(wb, sheet, null, i, scale, item.get("cgdh"), item.get("gsjc"), item.get("sydwjc"), item.get("fylb"),
                    item.get("jfsl"), item.get("je"), item.get("kdse"), item.get("dse"), item.get("jinge"));
        }
        HSSFCellStyle sumStyle = wb.createCellStyle();
        HSSFFont font = wb.createFont();
        font.setColor(HSSFFont.COLOR_RED);
        sumStyle.setFont(font);
        poiUtil.fillRow(sheet, sumStyle, result.size() + 1, "合计：", "", "", "", "", map.get("sum"), "", "", "");
        try {
            response.setContentType("application/x-download");
            String filedisplay = new String(("收发存物流估收明细.xls").getBytes("gb2312"), "iso8859-1");
            response.addHeader("Content-Disposition", "attachment;filename=" + filedisplay);
            wb.write(response.getOutputStream());
            response.getOutputStream().close();
        } catch (Exception e) {
            e.printStackTrace();
            renderText("位置异常，导出失败" + e.getMessage());
        }
        return null;
    }

    @Action("exportwljs")
    public String exportwljs() {
        Map<String, Map> map = cwsfcService.wljsExp(codeId, gsId, year, month);
        Page<Map> page = (Page) map.get("page");
        List<Map> result = page.getResult();
        HSSFWorkbook wb = new HSSFWorkbook();
        HSSFSheet sheet = wb.createSheet("sheet1");
        POIUtil poiUtil = new POIUtil(wb);
        sheet.setDefaultRowHeightInPoints(21);
        sheet.setDefaultColumnWidth((short) 20);
        HSSFCellStyle headerStyle = poiUtil.createHeader3HSSFCellStyle(wb);
        poiUtil.fillRow(sheet, headerStyle, 0, "采购单号", "公司名称", "收益单位", "费用类别", "数量", "总金额", "可抵税额", "抵税额", "净余额");
        List<Integer> scale = Arrays.asList(0, 0, 0, 0, 4, 2, 2, 2, 2);
        for (int i = 1; i < result.size() + 1; i++) {
            Map item = result.get(i - 1);
            poiUtil.fillRowComplex(wb, sheet, null, i, scale, item.get("cgdh"), item.get("gsjc"), item.get("sydwjc"), item.get("fylb"),
                    item.get("jfsl"), item.get("je"), item.get("kdse"), item.get("dse"), item.get("jinge"));
        }
        HSSFCellStyle sumStyle = wb.createCellStyle();
        HSSFFont font = wb.createFont();
        font.setColor(HSSFFont.COLOR_RED);
        sumStyle.setFont(font);
        poiUtil.fillRow(sheet, sumStyle, result.size() + 1, "合计：", "", "", "", "", "", "", "",
                map.get("sum"));
        try {
            response.setContentType("application/x-download");
            String filedisplay = new String(("收发存物流结算明细.xls").getBytes("gb2312"), "iso8859-1");
            response.addHeader("Content-Disposition", "attachment;filename=" + filedisplay);
            wb.write(response.getOutputStream());
            response.getOutputStream().close();
        } catch (Exception e) {
            e.printStackTrace();
            renderText("位置异常，导出失败" + e.getMessage());
        }
        return null;
    }

    /**
     * 销售结算明细
     */
    @Action("xsjs")
    public String xsjs() {
        if (year == null || month == null) {
            date = new Date();
        } else {
            Calendar c = Calendar.getInstance();
            c.set(Calendar.YEAR, year);
            c.set(Calendar.MONTH, month);
            c.set(Calendar.DAY_OF_MONTH, 0);
            date = c.getTime();
        }
        return SUCCESS;
    }

    @Action("xsjsList")
    public void xsjsList() {
        renderJson(cwsfcService.xsjsMx(codeId, gsId, year, month, page, rows));
    }

    @Action("exportxsjs")
    public String exportxsjs() {
        Map<String, Map> map = cwsfcService.xsjsExp(codeId, gsId, year, month);
        Page<Map> page = (Page) map.get("page");
        List<Map> result = page.getResult();
        HSSFWorkbook wb = new HSSFWorkbook();
        HSSFSheet sheet = wb.createSheet("sheet1");
        POIUtil poiUtil = new POIUtil(wb);
        sheet.setDefaultRowHeightInPoints(21);
        sheet.setDefaultColumnWidth((short) 20);
        HSSFCellStyle headerStyle = poiUtil.createHeader3HSSFCellStyle(wb);
        poiUtil.fillRow(sheet, headerStyle, 0, "销售单号", "公司名称", "物资编码", "品名", "规格", "单位", "数量",
                "成本总金额", "销售单价", "净余额", "税额", "客户");
        List<Integer> scale = Arrays.asList(0, 0, 0, 0, 0, 0, 4, 2, 4, 2, 2, 0);
        for (int i = 1; i < result.size() + 1; i++) {
            Map item = result.get(i - 1);
            poiUtil.fillRowComplex(wb, sheet, null, i, scale, item.get("xsdh"), item.get("gs"), item.get("wzcode"), item.get("wzbm"),
                    item.get("ggxh"), item.get("dw"), item.get("cksl"), item.get("cnje"),
                    item.get("xsdj"), item.get("je"), item.get("se"), item.get("kh"));
        }
        HSSFCellStyle sumStyle = wb.createCellStyle();
        HSSFFont font = wb.createFont();
        font.setColor(HSSFFont.COLOR_RED);
        sumStyle.setFont(font);
        poiUtil.fillRow(sheet, sumStyle, result.size() + 1, "合计：", "", "", "", "", "", "", ((Map) map.get("sum")).get("cnje"), "",
                ((Map) map.get("sum")).get("je"), "", "");
        try {
            response.setContentType("application/x-download");
            String filedisplay = new String(("收发存销售结算明细.xls").getBytes("gb2312"), "iso8859-1");
            response.addHeader("Content-Disposition", "attachment;filename=" + filedisplay);
            wb.write(response.getOutputStream());
            response.getOutputStream().close();
        } catch (Exception e) {
            e.printStackTrace();
            renderText("位置异常，导出失败" + e.getMessage());
        }
        return null;
    }

    /**
     * 领料结算明细
     */
    @Action("lljs")
    public String lljs() {
        if (year == null || month == null) {
            date = new Date();
        } else {
            Calendar c = Calendar.getInstance();
            c.set(Calendar.YEAR, year);
            c.set(Calendar.MONTH, month);
            c.set(Calendar.DAY_OF_MONTH, 0);
            date = c.getTime();
        }
        return SUCCESS;
    }

    @Action("lljsList")
    public void lljsList() {
        renderJson(cwsfcService.lldMx(codeId, gsId, year, month, page, rows));
    }

    @Action("exportlldmx")
    public String exportlldmx() {
        Map<String, Map> map = cwsfcService.lldExp(codeId, gsId, year, month);
        Page<Map> page = (Page) map.get("page");
        List<Map> result = page.getResult();
        HSSFWorkbook wb = new HSSFWorkbook();
        HSSFSheet sheet = wb.createSheet("sheet1");
        POIUtil poiUtil = new POIUtil(wb);
        sheet.setDefaultRowHeightInPoints(21);
        sheet.setDefaultColumnWidth((short) 20);
        HSSFCellStyle headerStyle = poiUtil.createHeader3HSSFCellStyle(wb);
        poiUtil.fillRow(sheet, headerStyle, 0, "领料单号", "开单日期", "领料单位", "领料部门", "领料人", "仓库名称", "物资编号", "品名",
                "规格", "图号", "单位", "数量", "总金额", "发料单位", "发料部门", "发料人", "制单人");
        List<Integer> scale = Arrays.asList(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 2, 4, 0, 0, 0, 0);
        for (int i = 1; i < result.size() + 1; i++) {
            Map item = result.get(i - 1);
            poiUtil.fillRowComplex(wb, sheet, null, i, scale, item.get("lldh"), item.get("kdrq"), item.get("llgs"), item.get("llbm"),
                    item.get("llr"), item.get("ck"), item.get("ggcode"), item.get("ggname"), item.get("ggxh"),
                    item.get("th"), item.get("dw"), item.get("flsl"), item.get("flje"), item.get("flgs"), item.get("flbm")
                    , item.get("flr"), item.get("zdr"));
        }
        HSSFCellStyle sumStyle = wb.createCellStyle();
        HSSFFont font = wb.createFont();
        font.setColor(HSSFFont.COLOR_RED);
        sumStyle.setFont(font);
        poiUtil.fillRow(sheet, sumStyle, result.size() + 1, "合计：", "", "", "", "", "", "", "", "", "", "", "",
                ((Map) map.get("footer")).get("je"), "", "", "", "");
        try {
            response.setContentType("application/x-download");
            String filedisplay = new String(("收发存内部领料明细.xls").getBytes("gb2312"), "iso8859-1");
            response.addHeader("Content-Disposition", "attachment;filename=" + filedisplay);
            wb.write(response.getOutputStream());
            response.getOutputStream().close();
        } catch (Exception e) {
            e.printStackTrace();
            renderText("位置异常，导出失败" + e.getMessage());
        }
        return null;
    }


    //=======getter setter=======
    public Long getGsId() {
        return gsId;
    }

    public void setGsId(Long gsId) {
        this.gsId = gsId;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public Integer getYear() {
        return year;
    }

    public void setYear(Integer year) {
        this.year = year;
    }

    public Integer getMonth() {
        return month;
    }

    public void setMonth(Integer month) {
        this.month = month;
    }

    public Date getStartDate() {
        return startDate;
    }

    public void setStartDate(Date startDate) {
        this.startDate = startDate;
    }

    public Date getEndDate() {
        return endDate;
    }

    public void setEndDate(Date endDate) {
        this.endDate = endDate;
    }

    public Date getDate() {
        return date;
    }

    public void setDate(Date date) {
        this.date = date;
    }

    public Integer getCodeId() {
        return codeId;
    }

    public void setCodeId(Integer codeId) {
        this.codeId = codeId;
    }

    public int getPage() {
        return page;
    }

    public void setPage(int page) {
        this.page = page;
    }

    public int getRows() {
        return rows;
    }

    public void setRows(int rows) {
        this.rows = rows;
    }

    public List<Gncz> getGnczList() {
        return gnczList;
    }

    public void setGnczList(List<Gncz> gnczList) {
        this.gnczList = gnczList;
    }

    @Autowired
    public void setGnczService(GnczService gnczService) {
        this.gnczService = gnczService;
    }

    @Autowired
    public void setCwsfcService(CwsfcService cwsfcService) {
        this.cwsfcService = cwsfcService;
    }

    @Autowired
    public void setGsjgService(GsjgService gsjgService) {
        this.gsjgService = gsjgService;
    }
}
